package com.example.com.daervideo.popuwindow;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.example.com.daervideo.R;
import com.example.com.daervideo.activitys.users.RechargeActivity;
import com.example.com.daervideo.fragments.video.VideoFragment;

/**
 * Created by i7-1 on 2018/12/8.
 */

public class GiftPpwindow extends PopupWindow {


    private final GiftAdapter giftAdapter;
    private final RecyclerView recyclerView;
    private View mCurrentItem;
    private int ID;

    public GiftPpwindow(Context context) {

        final View mMenuView = LayoutInflater.from(context).inflate(R.layout.popuwindow_gift, null);
        recyclerView = (RecyclerView) mMenuView.findViewById(R.id.gift_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        giftAdapter = new GiftAdapter(context);
        setListener(mMenuView, context);
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //适配虚拟键
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000); //android:background="#30000000"
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
    }

    private void setListener(View mMenuView, final Context context) {
        mMenuView.findViewById(R.id.recharge).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GiftPpwindow.this.dismiss();
                context.startActivity(new Intent(context, RechargeActivity.class));
            }
        });

        mMenuView.findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCurrentItem != null) {

                }
            }
        });
        mMenuView.findViewById(R.id.out).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GiftPpwindow.this.dismiss();
            }
        });
    }

    public void setData() {
        recyclerView.setAdapter(giftAdapter);
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    private class GiftAdapter extends RecyclerView.Adapter<GHolder> {

        private final Context context;


        public GiftAdapter(Context context) {
            this.context = context;
        }

        @Override
        public GHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_gift, parent, false);
            return new GHolder(inflate);
        }

        @Override
        public void onBindViewHolder(final GHolder holder, int position) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    holder.itemView.setBackgroundResource(R.drawable.bg_gift);
                    if (mCurrentItem != null) {
                        mCurrentItem.setBackground(null);
                    }
                    mCurrentItem = holder.itemView;
                }
            });

        }

        @Override
        public int getItemCount() {
            return 8;
        }
    }

    private class GHolder extends RecyclerView.ViewHolder {
        public GHolder(View inflate) {
            super(inflate);
        }
    }
}
